package org.zcl.map;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Package org.zcl.map
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2021/12/20 15:39
 */
public class MapSizeDemo {

    public static void main(String[] args) throws Exception {
        System.out.println("重地".hashCode());
        System.out.println("通话".hashCode());
        System.out.println("方面".hashCode());
        System.out.println("树人".hashCode());
        HashMap<String, String> map = new HashMap<>();

        System.out.println("当前HashMap的容量值为：" + getMapCapacity(map));
        map.put("1", "a");
        map.put("2", "b");
        map.put("3", "c");
        map.put("4", "d");
        map.put("5", "e");
        map.put("6", "f");
        map.put("7", "g");
        map.put("8", "h");
        map.put("9", "i");
        map.put("10", "j");
        map.put("11", "k");
        map.put("通话", "l");
        map.put("重地", "k");
        System.out.println("添加到*加载因子数量HashMap的容量值为：" + getMapCapacity(map));
        map.put("13", "m");
        System.out.println("添加超过*加载因子数量后HashMap的容量值为：" + getMapCapacity(map));

    }

    private static Integer getMapCapacity(HashMap<String, String> map) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method capacity = map.getClass().getDeclaredMethod("capacity");
        capacity.setAccessible(true);
        return (Integer) capacity.invoke(map);
    }
}
